package yunquan.wang.base.web.taglib;

import java.io.IOException;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import yunquan.wang.base.appcode.AppcodeHolder;
import yunquan.wang.modules.base.entity.CodeValue;

public class CodeSelectTaglib extends TagSupport {

	private String type;// 码表类型
	private String name;// Select 框的名字
	private Object value;// 选中的值
	private String css;// CSS值
	private String ref;// 下一级需要显示的的Select框的名字

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public String getRef() {
		return ref;
	}

	public void setRef(String ref) {
		this.ref = ref;
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		StringBuffer sbf = new StringBuffer();
		sbf.append("	<select class=\"" + css + "\" name=\"" + name + "\" " +" id=\""+name+"\" ");
		if (StringUtils.isNotBlank(ref)) {
			sbf.append(" ref=\"" + ref + "\" refUrl=\"" + "\"");
		}
		sbf.append(">");
		Set<CodeValue> codeValues = AppcodeHolder.getAllCodeValue(type);
		for (CodeValue codeValue : codeValues) {
			sbf.append("<option value=\"" + codeValue.getCode() + "\"");
			if (value != null && value.toString().equals(codeValue.getCode().toString())) {
				sbf.append(" selected ");
			}
			sbf.append(">" + codeValue.getValue() + "</option>");
		}
		sbf.append("</select>");
		try {
			out.print(sbf);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

}
